package cn.boyu.service.impl;

import cn.boyu.entity.AssetsTerminal;
import cn.boyu.entity.Gather;
import cn.boyu.entity.GatherAmmeter;
import cn.boyu.entity.SysDictData;
import cn.boyu.exception.LogicException;
import cn.boyu.mapper.GatherAmmeterMapper;
import cn.boyu.mapper.GatherMapper;
import cn.boyu.query.GatherQuery;
import cn.boyu.service.IAssetsTerminalService;
import cn.boyu.service.IGatherAmmeterService;
import cn.boyu.service.IGatherService;
import cn.boyu.service.ISysDictDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yn
 * @since 2022-11-22
 */
@Service
public class GatherServiceImpl extends ServiceImpl<GatherMapper, Gather> implements IGatherService {

    @Autowired
    private IAssetsTerminalService terminalService;
    @Autowired
    private GatherAmmeterMapper gatherAmmeterMapper;
    @Autowired
    private ISysDictDataService dataService;

    @Override
    public IPage<Gather> queryPage(GatherQuery qo) {
        IPage<Gather> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        QueryWrapper<Gather> qw = new QueryWrapper<>();
        qw.eq(qo.getTerminalId() != null, "terminal_id", qo.getTerminalId());
        IPage<Gather> result = page(page, qw);

        for (Gather gather : result.getRecords()) {
            AssetsTerminal terminal = terminalService.getById(gather.getTerminalId());
            List<SysDictData> companyType = dataService.getType("companyType", terminal.getCompanyType());
            if (companyType.size() != 0) {
                terminal.setCompanyTypeString(companyType.get(0).getDictLabel());
            }
            gather.setTerminal(terminal);
        }
        return result;
    }

    @Override
    public void saveGather(Gather gather) {
        //添加终端中的挂接标识
        UpdateWrapper<AssetsTerminal> qw = new UpdateWrapper<>();
        qw.eq("id", gather.getTerminalId());
        qw.set("mount_flg", 1);
        terminalService.update(qw);
        save(gather);
    }

    @Override
    public void delele(Long gatherId) {
        System.out.println("gatherId:"+gatherId);
        //判断采集信息下有没有挂接的电表
        List<GatherAmmeter> gather = gatherAmmeterMapper.selectList(new QueryWrapper<GatherAmmeter>().eq("gather_id", gatherId));
        System.out.println(gather);
        if (gather.size() == 0) {
            terminalService.update(new UpdateWrapper<AssetsTerminal>().set("mount_flg", 0).eq("id", getById(gatherId).getTerminalId()));
            remove(new QueryWrapper<Gather>().eq("id", gatherId));
        } else {
            throw new LogicException("此信息以及被挂接电表，不能删除");
        }
    }

    @Override
    public void edit(Gather gather) {
        updateById(gather);
    }


}
